<?php
namespace app\common\model;
use think\Db;
use think\Model;


class Notice extends Model
{
    private $table_name = 'wbe_notice';
    

    //验证条件
    protected $rule = [
        'sportsman'       =>  'require|max:50',
        
        'profile'   =>  'require|max:255',
        'sport_url'     =>  'require',
       
    ];

    //验证提示信息
    protected $message = [
        'sportsman.require'     => '用户名必须',
        'sportsman.max'         => '用户名最多不能超过50个字符',
        'profile.require'     => '简介名必须',
        'sportsman.max'         => '简介最多不能超过255个字符',

        'sport_url.require'     => '背景图必须',
       
      
    ];

    protected $scene = [
        'edit'  =>  ['user_name', 'user_name_en','user_accounts','entry_time','user_mobile','user_email'],
    ];

    protected function redis_name($com_id){
        return 'oa_user_list'.$com_id;

    }

    public function getNoticeInfo($_opt)
    {
        $data = $this
            ->allowField(true)
            ->where($_opt)
            ->find();
        return $data;
    }

    public function getNoticePage($_opt='', $field='*', $limit='10')
    {
        $data = Db::name('notice')->where($_opt)->field($field)->paginate($limit,false,['query'=>request()->param() ]);
        return $data;
    }
   
    public function getAllUser($com_id)
    {
        $redis_key = $this->redis_name($com_id);
        $redis  = new redis;
        $result = $redis->hGetAll($redis_key);
        if($result && count($result)>10) //数量<10,redis被清空过，从其它地方缓存进1个 举例：清空redis后,后台马上新增一个员工
        {
            $data = [];
            foreach ($result as $k=>$v)
            {
                $data[] = ['user_number'=>$k, 'user_name'=>$v];
            }
            return $data;
        }else
        {
            $data = Db::table($this->table_name)
                ->where(['user_status'=>1,'com_id'=>$com_id])
                ->field('user_name,user_number')
                ->select();

            foreach($data as $k=>$v)
            {
                $redis->hSet($redis_key, $v['user_number'], $v['user_name']);
            }
            return $data;
        }
    }

    public function addNoticeInfo($_opt)
    {

        $data = $this->allowField(true)->validate($this->rule,$this->message)->save($_opt);
        return $data;
    }

    public function updateInfo($_opt)
    {
        if(isset($_opt['id']))
        {
            $data = $this->allowField(true)->validate($this->rule,$this->message)->update($_opt);
        }

        return $data;
    }

    public function clearUserDepartId($depart_id){
        if($depart_id==''){
            return false;
        }
        $data = Db::table($this->table_name)->where("depart_id like '".$depart_id."%'")->update(['depart_id'=>'']);
        return $data;
    }

    public function getAllValidUsers(array $fields = []){
        if(empty($fields)){
            $fields = ['id','user_name','user_number','depart_id'];
        }
        $data = Db::table($this->table_name)->field($fields)->where(['user_status'=>1])->select();
        return $data;
    }

    public function deteteInfo($_opt)
    {
        $info = $this->allowField(true)->validate($this->rule,$this->message)->update($_opt);

        $this->updateRedis('hDel', $_opt['id']);

        return $info;
    }

    public function getnotice()
    {
       
        $data = Db::table("web_notice")
            ->order('id desc')
            ->limit(6)
            ->select();
        return $data;

       
    }

    public function findMember()
    {
        $_opt['user_status'] = array('neq',2);
        $data = $this
            ->where($_opt)
            ->field('user_number,user_name,schedule_id')
            ->select();

        $res = [];
        if(is_array($data)){
            foreach ($data as $key => $value){
                $res[] =$value->toArray();
            }
        }
        return $res;
    }

    /**
     * 获取userName值
     * @author Rocky Huang 2018-05-14
     * @param  int $user_number [description]
     * @return [type]        [description]
     */
    public function getRedisUserInfo($user_number = 0){
        $memberArray = $this->getUserInfoFromRedis();
        if($user_number>0){
            return $memberArray[$user_number];
        }
        return $memberArray;
    }





   
    /**
     * 查询员工转正时间  trans_time
     *
     */
    public function getAlltran ($_opt){

        $data = Db::table('oa_user_info')
            ->where($_opt)
            ->field('user_number,trans_time')
            ->select();
        return $data;
    }

    /**
     * 个人信息
     */
    public function getAllattend($_opt){
        $data = Db::table('oa_user_info')
            ->alias('a')
            ->where($_opt)
            ->join('oa_department b','b.depart_id = a.depart_id')
            //->join('oa_position c','c.pid = a.pid')
            ->field('a.user_number,a.user_name,a.schedule_id,b.depart_name')
            ->select();
        return $data;
    }
    /**
     * 查找说有的员工
     */
    public function getAllman(){
        $data = Db::table('oa_user_info')
            ->field('user_number,schedule_id')
            ->select();
        return $data;
    }
    /**
     * 查找某个人
     */
    public function getone($_opt){
        $data = Db::table('oa_user_info')
            ->where($_opt)
            ->find();
        return $data;
    }

    

}